package com.lsd.game.lastrun.model;

import java.util.Iterator;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.lsd.game.lastrun.Constants;

/*
 * This class defines the world that the world that the player navigates
 * through. All objects to be displayed should be declared and initalized
 * here AND should be added to objectList.
 */

public class World {

	/* Class members */
	
	// ObjectList holds ALL world objects. This simplifies drawing and updating
	private Array<AbstractObject> objectList = new Array<AbstractObject>();
	private Iterator<AbstractObject> listIterator;
	private Rock tempRock;
	private int numOfRocks = 0;
	private AbstractObject tempObj;
	private Player player;
	private double RNG;
	
	/* Class methods */
	
	//////////////////
	// CONSTRUCTORS //
	//////////////////
	
	// Just calls generateWorld()
	public World() {
		generateWorld();
	}
	
	/////////////////
	// GET METHODS //
	/////////////////
	
	public Array<AbstractObject> getObjects() { return objectList; }
	
	private void generateWorld() {
		player = new Player(new Vector2(0.5f,5f), Constants.PLAYER_SIZE, Constants.PLAYER_SPEED, "player_01");
		objectList.add(player);
		
		float randX, randY, randSize, randSpeed;
		
		for (int i = 0; i < 10; i++) {
			randX = (int) (Math.random() * 16);
			randY = (int) (Math.random() * 9);
			randSize = (float) (Math.random() * 2 + 0.2f);
			randSpeed = (float) (Math.random() * 5 + 3);
			tempRock = new Rock(new Vector2(randX, randY), randSize, randSpeed, "rock_02");
			objectList.add(tempRock);
			numOfRocks++;
		}
	}

	///////////////////
	// UPDATE METHOD //
	///////////////////
	
	// Update objects and stuff here
	public void update(float delta) {
		
		RNG = Math.random();
		if (RNG < 0.05 & numOfRocks < 10 ) {
			objectList.add(new Rock(new Vector2(20, (int) (Math.random() * 9)), (float) (Math.random() * 2 + 0.2f), (float) (Math.random() * 5 + 3), "rock_01"));
			numOfRocks++;
		}
		
		// Update all world objects
		listIterator = objectList.iterator();
		while (listIterator.hasNext()) {
			tempObj = listIterator.next();
			tempObj.update(delta);
			
			// Remove rocks when off screen
			if (tempObj.getClass().getSimpleName().equals("Rock") & tempObj.getPosition().x < -5) {
				listIterator.remove();
				numOfRocks--;
			}
			
		}
	}
}
